<template>
  <div class="news-container">
    <el-card class="news-card">
      <div class="img-container">
        <el-carousel height="300px">
          <el-carousel-item v-for="item in imgList" :key="item.id">
            <div class="banner-box">
              <img :src="getImg(item.src)" alt="" />
            </div>
            <!-- <img :src="getImg(item.src)" alt="" /> -->
          </el-carousel-item>
        </el-carousel>
      </div>
      <div class="body">
        <div class="img-container">
          <span class="title">图说恒星</span>
          <div class="img-box">
            <div class="img-item" v-for="item in imgNewsList" :key="item.id" @click="showDialog(item)">
              <img :src="getImg(item.img)" />
              <p class="flag">{{ item.title }}</p>
            </div>
          </div>
        </div>
        <!-- 图说恒星对话框 -->
        <el-dialog
          class="static-dialog"
          :title="imgNewsList[flag].title"
          v-if="imgNewsList"
          :visible.sync="dialogVisible"
          width="50%"
        >
          <div class="describe">
            {{ imgNewsList[flag].desc }}
          </div>
        </el-dialog>
        <!-- 校园资讯 -->
        <div class="school-information">
          <span class="title">校园资讯</span>
          <div class="info-item" v-for="item in this.newsListFromServe" :key="item.id" @click="handleNews(item)">
            <span class="news-title">{{ item.newsTitle }}</span>
            <div class="info">
              <span class="userName">{{ item.userName }}</span>
              <span>{{ item.createTime }}</span>
            </div>
          </div>
          <!-- 校园资讯对话框 -->
          <el-dialog :title="this.infoTitle" :visible.sync="newsDialogVisible" width="90%">
            <div v-html="infoItem">
              {{ infoItem }}
            </div>
          </el-dialog>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import staticServe from '../api/service/static.serve'
import commonServe from '../api/service/common.serve'
export default {
  data() {
    return {
      dialogVisible: false,
      flag: 0,
      imgList: [
        { id: 1, src: 'pic1.png' },
        { id: 2, src: 'pic2.jpg' },
        { id: 3, src: 'pic3.jpg' }
      ],
      imgNewsList: [],
      newsListFromServe: [],
      newsDialogVisible: false,
      infoTitle: '',
      infoItem: ''
    }
  },
  created() {
    this.getLocalNewsList()
  },
  methods: {
    getImg(src) {
      return require('@/assets/img/news/' + src)
    },

    getLocalNewsList() {
      // 本地新闻资源
      staticServe.getNews().then((res) => {
        console.log(res)
        this.imgNewsList = res
      })
      // 服务器资源
      commonServe
        .getNewsList()
        .then((res) => {
          if (res.code === 200) {
            this.newsListFromServe = res.data.list
            console.log(this.newsListFromServe)
          } else {
            this.$message.warning('获取新闻内容失败')
          }
        })
        .catch((err) => {
          this.$message.warning(err.message)
        })
    },
    // 点击校园资讯对话框
    handleNews(val) {
      this.newsDialogVisible = true
      console.log(val)
      this.infoTitle = val.newsTitle
      // 转码
      var decodeNewsDesc = val.newsDesc.replace(/&lt;/g, '<')
      var decodeNewsDesc = decodeNewsDesc.replace(/&gt;/g, '>')
      this.infoItem = decodeNewsDesc
    },
    showDialog(val) {
      this.dialogVisible = true
      console.log(val)
      this.flag = val.id
    }
  }
}
</script>

<style lang="less" scoped>
.news-container {
  .news-card {
    .body {
      .img-container {
        // border: 1px solid black;
        .el-carousel {
          // .el-carousel-item {
          // display: flex;
          // align-items: center;
          // .banner-box {
          // height: 340px;
          // width: 1000px;
          img {
            width: 100%;
            height: auto;
          }
          // }
          // }
        }
        padding-top: 20px;
        .title {
          border-radius: 2px;
          display: block;
          background-color: #3a62d7;
          color: #ececec;
          font-size: 24px;
          font-weight: bold;
          letter-spacing: 10px;
          font-weight: bold;
          text-shadow: 2px 2px 5px #999;
        }
        .img-box {
          padding-top: 12px;
          // border: 1px solid black;
          display: flex;
          flex-wrap: wrap;
          align-content: flex-start;
          align-items: center;
          justify-content: space-between;

          .img-item {
            margin-top: 40px;
            margin-bottom: 20px;
            width: 100%;
            height: 300px;
            flex: 0 0 30%;
            display: flex;
            align-items: center;
            justify-content: space-between;
            flex-direction: column;
            box-sizing: border-box;
            border: 2px solid #ececec;
            border-radius: 5px;
            img {
              width: 100%;
              height: 250px;
              border-radius: 5px;
            }
            .flag {
              font-size: 18px;
              color: #999;
              font-weight: bold;
            }
          }
          .img-item:hover {
            cursor: pointer;
            border: 2px solid #3a62d7;
            transform: translateY(-10px);
            transition: 0.5s;
            box-shadow: #ccc 0px 10px 10px;
          }
        }
      }
      .static-dialog {
        .describe {
          font-weight: normal;
          font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
          letter-spacing: 2px;
          text-align: justify;
        }
      }
      .school-information {
        .title {
          border-radius: 2px;
          display: block;
          background-color: #3a62d7;
          color: #ececec;
          font-size: 24px;
          font-weight: bold;
          letter-spacing: 10px;
          font-weight: bold;
          text-shadow: 2px 2px 5px #999;
        }
        .info-item {
          border-radius: 5px;
          margin-top: 20px;
          border: 2px solid #ececec;
          .news-title {
            height: 50px;
            font-weight: bold;
            padding-left: 20px;
            font-size: 18px;
            // color: #999;
            display: flex;
            flex-direction: column;
            justify-content: space-around;
          }
          .info {
            color: #999;
            display: flex;
            span {
              display: block;
            }
            .userName {
              margin-right: 30px;
            }
            padding-left: 20px;
          }
        }
        .info-item:hover {
          cursor: pointer;
          border: 2px solid #3a62d7;
          transform: translateY(-8px);
          transition: 0.5s;
          box-shadow: #ccc 0px 10px 10px;
        }
      }
    }
  }
}
</style>
